<template>
  <div class="page-bgBack">
    <rTitle :type="'back'" :tit="`${$route.name == 'talentEdit' ? '编辑信息' : '新建人才'}`" />
    <showResume
      :status="`${$route.name == 'talentEdit' ? 'edit' : 'add'}`"
      :companyUnitsInfo="$route.name == 'talentEdit' ? { talentPoolId: $route.query.talentPoolId } : {}"
      @handleSubmit="handleSubmit"
      @editCancel="$router.go(-1)"
    ></showResume>
  </div>
</template>

<script>
import { mapState } from "vuex";
import moment from "moment";
import { getNormalPost, putJobSeekers2, putJobSeekers, getInfoById, getTalentPools2 } from "../api";
import showResume from "../../talent/msg/component/showResume/index.vue";
import rTitle from "../../component/rTitle";
export default {
  components: {
    rTitle,
    showResume
  },
  data() {
    const validatorPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入手机号!"));
      } else {
        if (!/^1[3456789]\d{9}$/.test(value)) {
          callback(new Error("手机号格式不正确!"));
        } else {
          callback();
        }
      }
    };
    return {
      intentionJob: [],
      workYearsCheckbox: false,
      workYearsNum: "",
      validatorPhone,
      replaceFields: {
        children: "childList",
        key: "id",
        value: "id",
        title: "name"
      },
      workArr: [],
      form: this.$form.createForm(this, {}),
      socCity: "",
      addressCodeCopy: null,
      jobName: "",
      numArr: [
        { type: "1", name: "待处理", num: 74 },
        { type: "2", name: "已投递", num: 74 },
        { type: "3", name: "已约面", num: 74 },
        { type: "4", name: "offer沟通", num: 74 },
        { type: "5", name: "已入职", num: 74 }
      ],
      src: "",
      afterConversionAnnex: "",
      currentPage: 1,
      pageCount: 0,
      intentionJobCopy: [],
      intentionJobText: "",
      companyUnitsId: "",
      intentionJobVal: ""
    };
  },
  computed: {
    ...mapState(["admin", "hr"])
  },
  created() {
    this.onGetRecruitInfos();
    if (this.$route.query.talentPoolId) {
      this.onGetJobSeekers();
    }
    if (this.$route.query.deliverRecordId) {
      this.onGetInfoById();
    }
  },
  mounted() {},
  watch: {
    workYearsNum(val) {
      if (val) {
        this.workYearsCheckbox = false;
      }
    }
  },
  methods: {
    disabledDate(current) {
      return current && current > moment().endOf("day");
    },
    onChange(e) {
      if (e.target.checked) {
        this.workYearsNum = "";
        this.form.setFieldsValue({ workYears: null });
      }
    },
    moment,
    displayRender({ labels }) {
      return labels[labels.length - 1];
    },
    async onGetRecruitInfos() {
      try {
        const { data } = await getNormalPost({
          "orders[0].property": "gmtCreate",
          "orders[0].desc": true,
          pageNo: 1,
          pageSize: 1000
        });
        this.workArr = data.entities;
      } catch (err) {
        console.log("error log --> ", err);
      }
    },
    async onGetInfoById() {
      try {
        const { data } = await getInfoById(this.$route.query.deliverRecordId);
        this.form.setFieldsValue({
          postId: data.recruitInfoId
        });
      } catch (err) {
        console.log("error log --> ", err);
      }
    },
    async onGetJobSeekers() {
      try {
        const { data } = await getTalentPools2(this.$route.query.talentPoolId);
        console.log(data);
        this.intentionJobVal = data.customTemplateMap.intentionJob;
        data.customTemplateMap.intentionJob = data.customTemplateMap.intentionJob ? data.customTemplateMap.intentionJob.split("*") : [];
        var arr = [];
        this.intentionJob = [];
        if (data.customTemplateMap.intentionJob && data.customTemplateMap.intentionJob.length) {
          arr = data.customTemplateMap.intentionJob[0].split(",");
          if (arr.length === 9) {
            this.intentionJob.push(arr[2], arr[5], arr[8]);
          } else if (arr.length === 6) {
            this.intentionJob.push(arr[2], arr[5]);
          } else {
            this.intentionJob.push(arr[2]);
          }
        }
        this.intentionJobCopy = this.intentionJob;
        console.log(this.intentionJob, this.intentionJobCopy);
        setTimeout(() => {
          this.form.setFieldsValue({
            name: data.customTemplateMap.name,
            phone: data.customTemplateMap.phone,
            gender: data.customTemplateMap.gender,
            address: data.customTemplateMap.address,
            skill: data.customTemplateMap.skill ? data.customTemplateMap.skill.split(",") : undefined,
            remark: data.customTemplateMap.remark,
            workExperience: data.customTemplateMap.workExperience,
            addressCode: data.customTemplateMap.addressCode,
            birthday: data.customTemplateMap.birthday ? moment(data.customTemplateMap.birthday, "YYYY-MM-DD") : undefined,
            intentionJob: this.intentionJob,
            workYears: data.customTemplateMap.workYears === "0" ? null : data.customTemplateMap.workYears
          });
          this.workYearsCheckbox = data.customTemplateMap.workYears === "0";
          this.addressCodeCopy = data.customTemplateMap.addressCode
            ? data.customTemplateMap.addressCode.split(",")[data.customTemplateMap.addressCode.split(",").length - 1]
            : "";
          this.socCity = data.customTemplateMap.addressCode;
          this.src = data.customTemplateMap.annex;
        });
      } catch (err) {
        console.log("error log --> ", err);
      }
    },
    async onPostAddCandidate(v) {
      try {
        await putJobSeekers2({ customTemplateMap: v });
        this.$notification.success({ message: "操作成功" });
        this.$router.go(-1);
      } catch (err) {
        console.log("error log --> ", err);
      }
    },
    async onPutJobSeekers(v) {
      try {
        await putJobSeekers({
          id: this.$route.query.talentPoolId,
          customTemplateMap: v
        });
        this.$notification.success({ message: "操作成功" });
        this.$router.go(-1);
      } catch (err) {
        console.log("error log --> ", err);
      }
    },
    handleSubmit(values) {
      if (values.workExperience && values.workExperience.length < 10) {
        this.$message.error("工作经历最少输入10个字！");
        return;
      } else if (values.workExperience && values.workExperience.length > 520) {
        this.$message.error("工作经历最多输入520个字！");
        return;
      }
      values.companyUnitsId = this.companyUnitsId;
      values.workYears = values.workYears && values.workYears === 0 ? 0 : values.workYears;
      var s = true;
      if (values.skill) {
        if (values.skill.length > 3) {
          this.$message.error("擅长技能最多只能填三个");
          s = false;
        }
        values.skill.some((e) => {
          if (e.length > 6) {
            this.$message.error("每个标签长度不能超过6位");
            s = false;
            return true;
          }
        });
      }
      console.log("values", values);
      if (s) {
        values.skill = values.skill ? values.skill.join(",") : "";
        if (this.$route.name === "talentEdit") {
          this.onPutJobSeekers(values);
        } else {
          this.onPostAddCandidate(values);
        }
      }
    },
    handleCity(e) {
      console.log(e);
      if (e.value.length > 0) {
        const value = e.value;
        const label = e.label;
        this.socCity = value + "";
        if (label.length && label.length === 3) {
          this.socCityName = label[0] + "/" + label[1] + "/" + label[2];
        } else if (label.length && label.length === 2) {
          this.socCityName = label[0] + "/" + label[1];
        }
      } else {
        this.socCity = "";
        this.socCityName = "";
      }
    },
    upData(e) {
      this.src = e.transformationFrontUrl;
      this.afterConversionAnnex = e.transformationResultUrl || e.transformationFrontUrl;
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    }
  }
};
</script>

<style lang="scss" scoped>
.r-add-box {
  padding: 24px 24px 48px 24px;
  .sumbit {
    position: fixed;
    right: 30px;
    bottom: 20px;
    width: calc(100% - 236px);
    height: 56px;
    line-height: 56px;
    background: #fff;
    text-align: center;
    border-top: 1px solid #e8e8e8;
  }
  .pdfNull {
    width: 534px;
    height: 774px;
    background: #f4f6fa;
    border-radius: 4px;
    text-align: center;
    padding-top: 168px;
  }
}
</style>
